1 using UnityEngine;
2 using System.Collections;
3 using UnityEngine.UI;
4
5 public class timer : MonoBehaviour
6 {
7 public Text timerLabel;
8 public Text lapsLabel;
9 public Text lapscounter;
10
11 public Text entertostart;
12 public Text finallaptext;
13
14 public GameObject place;
15 public AudioSource[] beeps;
16
17 public AudioClip backmusic;
18 public AudioClip racestart;
19 public AudioClip winrace;
20 public AudioClip loserace;
21 public AudioClip finallap;
22
23 public RawImage semaphore;
24
25 public Material sem0;
26 public Material sem1;
27 public Material sem2;
28 public Material sem3;
29
30 public int wincount = 0;
31
32 public Font myfont;
33
34 public CanvasRenderer cr;
35
36 public int laps = 1;
37 public int currentlap = 1;
38
39 private float time = 0;
40
41 public bool startcount = false;
42
43 public float count = 0;
44
45 public bool startrace = false;
46
47 private bool finished = false;
48
49 private int audioplayed = 0;
50
51 float minutes;
52 float seconds;
53 float fraction;
54
55 void Start()
56 {
57 beeps = place.GetComponents<AudioSource>();
58 beeps[4].PlayOneShot(racestart);
59 }
60
61 void Update()
62 {
63 if (!startrace && Input.GetAxis("Submit") == 1)
64 {
65 startcount = true;
66
67 }
68 if (startcount)
69 {
70 if (count < 1f)
71 {
72 semaphore.enabled = true;
73 semaphore.material = sem0;
74 }
75 else if (count < 2f)
76 {
77 if (audioplayed == 0)
78 {
79 beeps[0].Play();
80 audioplayed++;
81 }
82 semaphore.material = sem1;
83 }
84 else if (count < 3f)
85 {
86 if (audioplayed == 1)
87 {
88 beeps[1].Play();
89 audioplayed++;
90 }
91 semaphore.material = sem2;
92 }
93 else if (count < 4f)
94 {
95 if (audioplayed == 2)
96 {
97 beeps[2].Play();
98 audioplayed++;
99 }
100 semaphore.material = sem3;
101 startrace = true;
102 }
103 else if (count < 4.5f)
104 {
105 if (audioplayed == 3)
106 {
107 beeps[3].loop = true;
108 beeps[3].PlayOneShot(backmusic);
109 audioplayed++;
110 }
111 }
112 else if (count < 5.5f)
113 {
114 startcount = false;
115 audioplayed = 0;
116 count = 0;
117 semaphore.enabled = false;
118 }
119 }
120
121 if (Input.GetAxis("Restart") == 1){
122 for (int i = 0; i < beeps.Length; i++)
123 {
124 beeps[i].Stop();
125 }
126 startrace = false;
127 finished = false;
128 wincount = 0;
129 currentlap = 1;
130 time = 0;
131
132 int childs = cr.transform.childCount;
133 for (int i = childs - 1; i >= 0; i--)
134 {
135 Destroy(cr.transform.GetChild(i).gameObject);
136 }
137
138 BroadcastMessage("resetIt");
139 }
140
141 if (startcount)
142 {
143 count += Time.deltaTime;
144 }
145
146
147 if (startrace)
148 {
149 time += Time.deltaTime;
150 }
151
152 minutes = Mathf.Floor(time / 60);
153 seconds = Mathf.Floor(time) % 60;
154 fraction = Mathf.Floor(time * 100) % 100;
155
156 if (!finished)
157 {
158 timerLabel.text = string.Format("{0:00} : {1:00} : {2:00}", minutes, seconds, fraction);
159 lapscounter.text = "Lap " + currentlap + " / " + laps;
160 }
161
162 if (!finished && !startrace && !startcount)
163 {
164 entertostart.enabled = true;
165 }
166 else
167 {
168 entertostart.enabled = false;
169 }
170
171 }
172 public void addlap(Transform who,int lap,Color color)
173 {
174 if (lap == laps + 1)
175 {
176 crossGoal(who, color);
177 }
178
179 if (who.name == "Car")
180 {
181 if (lap == laps + 1)
182 {
183 if (wincount < 4)
184 {
185 beeps[3].Stop();
186 beeps[4].PlayOneShot(winrace);
187 }
188 else
189 {
190 beeps[3].Stop();
191 beeps[4].PlayOneShot(loserace);
192 }
193 finished = true;
194 }
195 else if (lap < laps + 1)
196 {
197 currentlap = lap;
198 lapsLabel.text = timerLabel.text + "\n" + lapsLabel.text;
199 if (lap == laps)
200 {
201 //final lap
202 pausemusicbriefly(2.5f);
203 beeps[4].PlayOneShot(finallap);
204 finallaptext.text = "Final Lap";
205 finallaptext.enabled = true;
206 Invoke("removemes", 2);
207 }
208 }
209 }
210
211 }
212
213 public void pausemusicbriefly(float time)
214 {
215 beeps[3].Pause();
216 Invoke("unpausemusic", time);
217 }
218
219 public void removemes()
220 {
221 finallaptext.enabled = false;
222 }
223
224 public void unpausemusic()
225 {
226 beeps[3].UnPause();
227 }
228
229 public void crossGoal(Transform who, Color color)
230 {
231 GameObject newGO = new GameObject("myTextGO");
232 newGO.transform.SetParent(cr.transform);
233
234 Text myText = newGO.AddComponent<Text>();
235 Vector3 pos = new Vector3(35, 145 - 20 * wincount, 0);
236 myText.transform.localPosition = pos;
237 wincount++;
238 myText.font = myfont;
239 myText.fontStyle = FontStyle.Bold;
240 myText.alignment = TextAnchor.UpperRight;
241 myText.color = color;
242 myText.text = (wincount)+ ". " + string.Format("{0:00} : {1:00} : {2:00}", minutes, seconds, fraction);;
243 }
244
245 }